<?php

class Reportes_PromocionesController extends Zend_Controller_Action
{
    /**
     * 
     * @var Zend_Auth_Storage_Interface
     */
    private $_user_info;
    /**
     * 
     * @var Zwei_Admin_Acl
     */
    private $_acl;
    /**
     * 
     * @var string
     */
    private $_page;

    public function init()
    {
        $this->_helper->layout()->disableLayout();
        if (Zend_Auth::getInstance()->hasIdentity()) {
            $this->_user_info = Zend_Auth::getInstance()->getStorage()->read();
            $this->_acl = new Zwei_Admin_Acl($this->_user_info->user_name);
            $this->_page = "reportes/bolsas";
            if (!$this->_acl->isUserAllowed($this->_page)) exit("<p>Acceso denegado a $this->_page</p>"); 
        } else {
            $this->_redirect('index/login');
        }
    }

	public function indexAction()
	{
		// action body
	}

	public function eliminarAction()
	{
		//echo "Se va a enviar: [".$_REQUEST["msisdn"]."][".$_REQUEST["id_promo"]."]";
		//exit;
		$xmlcontent = "<?xml version='1.0'  encoding='ISO-8859-1'?>";
		$xmlcontent .= "<msg>";
		$xmlcontent .= "<header id_trans='1111' app='xml' user='web' passw='web' action='1'/>";
		$xmlcontent .= "<req>";
		$xmlcontent .= "<op>req_elim_promo</op>";
		$xmlcontent .= "<msisdn>".$_REQUEST["msisdn"]."</msisdn>";
		$xmlcontent .= "<idPromo>".$_REQUEST["id_promo"]."</idPromo>";
		$xmlcontent .= "</req>";
		$xmlcontent .= "</msg>";

		global $urlServerXML;
		$url = $urlServerXML;

		//echo $url;
		sleep(1);

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_VERBOSE, 0);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_POST, 1);
		//curl_setopt($ch, CURLOPT_PUT, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlcontent);

		$result = curl_exec($ch);
		//echo "Llega: [".$result."]<br>";

		if (curl_errno($ch))
		{
			curl_close($ch);
			echo "<script  type='text/javascript'>
		                alert('No se pudo eliminar la recurrencia');
		                //window.location = '../pl_reportes/cons_online.php?consultar=Consultar&msisdn=".$_REQUEST["msisdn"]."';
		              </script>";
		}
		else
		{
			curl_close($ch);
			if( $this->procesaXML($result) )
			{
				echo "<script  type='text/javascript'>
		                alert('La recurrencia fue eliminada con exito.');
		                window.parent.cargarPanelCentral('reportes/cons-online?consultar=Consultar&msisdn=".$_REQUEST["msisdn"]."';
		              </script>";
			}
			else
			echo "<script  type='text/javascript'>
		                alert('No se pudo eliminar la recurrencia.');
		                //window.location = '../pl_reportes/cons_online.php?consultar=Consultar&msisdn=".$_REQUEST["msisdn"]."';
		              </script>";
				
		}
		 
	}

	private function procesaXML($result)
	{
		include_once ROOT_DIR."/classes/class_xml.php";
		$xml = new SimpleXMLElementExtended($result);
		if( !is_object($xml) )
		{
			echo "Error XML";
			return false;
		}

		//    echo "RESULT:  ".(string)$xml->resp->result;
		//    echo "STRRESULT:  ".(string)$xml->resp->strresult;

		if( (string)$xml->resp->result == 0 )
		return true;
		else
		return false;
	}

	public function comprarAction()
	{

		if($_SERVER['REQUEST_METHOD'] == 'GET') {
			//print_r($_REQUEST);
			//$la_pe=$_REQUEST['p'];
			//echo "lape: ". $la_pe . "<br>";
			echo "<script>window.parent.cargarPanelCentral('../{$_SERVER['REQUEST_URI']}') </script>";
			//echo $_SERVER['REQUEST_METHOD']."<br>";
		}
	  
		openlog("PROMOCIONES", LOG_PID | LOG_PERROR, LOG_LOCAL6);
		syslog(LOG_DEBUG, "COMPRA: (msisdn=". $_REQUEST["msisdn"] .")(id_promo=".$_REQUEST["id_promo"].")");

		$xmlcontent = "<?xml version='1.0'  encoding='ISO-8859-1'?>";
		$xmlcontent .= "<msg>";
		$xmlcontent .= "<header id_trans='1111' app='xml' user='web' passw='web' action='1'/>";
		$xmlcontent .= "<req>";
		$xmlcontent .= "<op>req_comp_promo</op>";
		$xmlcontent .= "<msisdn>".$_REQUEST["msisdn"]."</msisdn>";
		$xmlcontent .= "<idPromo>".$_REQUEST["id_promo"]."</idPromo>";
		$xmlcontent .= "</req>";
		$xmlcontent .= "</msg>";

		global $urlServerXML;
		$url = $urlServerXML;

		//echo $url;
		//sleep(1);

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_VERBOSE, 0);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_POST, 1);
		//curl_setopt($ch, CURLOPT_PUT, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlcontent);

		$result = curl_exec($ch);
		//echo "Llega: [".$result."]<br>";

		if (curl_errno($ch))
		{
			syslog(LOG_DEBUG, "COMPRA: Error curl_errno");
			curl_close($ch);
			echo "<script  type='text/javascript'>
                alert('No se pudo realizar la compra');
                //window.location = '../pl_reportes/compras.php?consultar=Consultar&msisdn=".$_REQUEST["msisdn"]."';

              </script>";
		}
		else
		{
			curl_close($ch);
			if( procesaXML($result) )
			{
				syslog(LOG_DEBUG, "COMPRA: Exitosa");
				echo "<script  type='text/javascript'>
                alert('La compra fue realizada exitosamente');
                window.parent.cargarPanelCentral('reportes/cons-online?consultar=Consultar&msisdn=".$_REQUEST["msisdn"]."';
              </script>";
			}
			else
			{
				syslog(LOG_DEBUG, "COMPRA: Error");
				echo "<script  type='text/javascript'>
                alert('No se pudo realizar la compra');
                //window.location = '../pl_reportes/compras.php?consultar=Consultar&msisdn=".$_REQUEST["msisdn"]."';
              </script>";

			}
		}
		closelog();
	  
	}


}



